package samfeng.erp.product.specification.controller.admin;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import samfeng.erp.product.specification.controller.admin.vo.*;
import samfeng.erp.product.specification.convert.SpecificationConvert;
import samfeng.erp.product.specification.dal.dataobject.SpecificationDO;
import samfeng.erp.product.specification.service.SpecificationService;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 产品规格")
@RestController
@RequestMapping("/product/specification")
@Validated
public class SpecificationController {

    @Resource
    private SpecificationService specificationService;

    @PostMapping("/create")
    @ApiOperation("创建产品规格")
    @PreAuthorize("@ss.hasPermission('product:specification:create')")
    public CommonResult<Long> createSpecification(@Valid @RequestBody SpecificationCreateReqVO createReqVO) {
        return success(specificationService.createSpecification(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新产品规格")
    @PreAuthorize("@ss.hasPermission('product:specification:update')")
    public CommonResult<Boolean> updateSpecification(@Valid @RequestBody SpecificationUpdateReqVO updateReqVO) {
        specificationService.updateSpecification(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除产品规格")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:specification:delete')")
    public CommonResult<Boolean> deleteSpecification(@RequestParam("id") Long id) {
        specificationService.deleteSpecification(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得产品规格")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:specification:query')")
    public CommonResult<SpecificationRespVO> getSpecification(@RequestParam("id") Long id) {
        SpecificationDO specification = specificationService.getSpecification(id);
        return success(SpecificationConvert.INSTANCE.convert(specification));
    }

    @GetMapping("/list")
    @ApiOperation("获得产品规格列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:specification:query')")
    public CommonResult<List<SpecificationRespVO>> getSpecificationList(@RequestParam("ids") Collection<Long> ids) {
        List<SpecificationDO> list = specificationService.getSpecificationList(ids);
        return success(SpecificationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得产品规格分页")
    @PreAuthorize("@ss.hasPermission('product:specification:query')")
    public CommonResult<PageResult<SpecificationRespVO>> getSpecificationPage(@Valid SpecificationPageReqVO pageVO) {
        PageResult<SpecificationDO> pageResult = specificationService.getSpecificationPage(pageVO);
        return success(SpecificationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出产品规格 Excel")
    @PreAuthorize("@ss.hasPermission('product:specification:export')")
    @OperateLog(type = EXPORT)
    public void exportSpecificationExcel(@Valid SpecificationExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SpecificationDO> list = specificationService.getSpecificationList(exportReqVO);
        // 导出 Excel
        List<SpecificationExcelVO> datas = SpecificationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品规格.xls", "数据", SpecificationExcelVO.class, datas);
    }

}
